#include "student.h"
#include "ui_student.h"
#include "rootmenu.h"

#include <QMessageBox>
#include <QPushButton>
#include <QDebug>
#include <QLine>
#include <QRegExp>
#include <QCursor>

student::student(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::student)
{
    ui->setupUi(this);
    ui->centralwidget->setStyleSheet("background:transparent");
}

student::~student()
{
    delete ui;
}

void student::on_Btn_Update_clicked()
{
    setCursor(Qt::BusyCursor);
    if(modify){ //确定修改
        QString name = this->ui->Line_Name->text();
        QString tel = this->ui->Line_Phone->text();
        QString pwd = this->ui->Line_Pwd->text();
        QString pwd_2 = this->ui->Line_Pwd_2->text();
        QString content = "姓名： " + name + "\n" + "电话： " + tel + "\n" + "密码： " + pwd;

        //姓名检测（1~5个汉字）
        bool name_error = !name.contains(QRegExp("^[\u4E00-\u9FA5]{1,5}$")); //全中文为false
        if(name.length() < 1)
            name_error = true;
        //qDebug() << "name" << name_error;

        //tel检测(11位纯数字)
        bool tel_error = tel.contains(QRegExp("[\\x20-\\x2f | \\x3a-\\x7e]+"));  //含非数字为true
        if(tel.length() != 11)
            tel_error = true;
        //qDebug() << "tel" << tel_error;

        //密码检测(6~10为不含空格的字符串)
        bool pwd_error = pwd.contains(QRegExp("[\\x20]+"));  //含空格为true
        if(pwd.length() < 6)
            pwd_error = true;
        //qDebug() << "pwd" << pwd_error;

        if(name_error){
            setCursor(Qt::ArrowCursor);
            QMessageBox::critical(this, "ERROR", "姓名填写错误（1~5个汉字）", "确定");
        }
        else if(tel_error){
            setCursor(Qt::ArrowCursor);
            QMessageBox::critical(this, "ERROR", "电话填写错误（11位纯数字）", "确定");
        }
        else if(pwd_error){
            setCursor(Qt::ArrowCursor);
            QMessageBox::critical(this, "ERROR", "密码填写错误（6~10位非空格字符）", "确定");
        }
        else if(pwd != pwd_2){
            setCursor(Qt::ArrowCursor);
            QMessageBox::critical(this, "ERROR", "两次密码输入不一致", "确定");
        }
        else{
            setCursor(Qt::ArrowCursor);
            int ret = QMessageBox::warning(this, "请确认信息", content, "确认", "取消");
            if(!ret){
                setCursor(Qt::BusyCursor);
                QString update_user = QString("UPDATE users SET Password = '%1' "
                                              "WHERE Account = '%2'").arg(pwd).arg(UID);
                QString update_stu = QString("UPDATE students SET Name = '%1', Phone = '%2'"
                                             "WHERE UID = '%3'").arg(name).arg(tel).arg(UID);
                QSqlQuery update;
                if(update.exec(update_stu) == false){
                    setCursor(Qt::ArrowCursor);
                    QMessageBox::critical(this, "ERROR", "所填信息不合法", "确定");
                }
                else{
                    update.exec(update_user);
                    setCursor(Qt::ArrowCursor);
                    QMessageBox::information(this, "SUCCESS", "修改成功", "确定");
                    modify = false;
                }
            }
        }
    }
    else{   //进入修改状态
        modify = true;
    }
    this->showinfo();
}

void student::showinfo()
{
    //将之前的用户数据填写到表单中
    QString sqlstr = RootMenu::selectstr(2, UID);
    QSqlQuery query;
    setCursor(Qt::BusyCursor);
    query.exec(sqlstr);
    query.next();

    this->ui->Line_Name->setText(query.value("Name").toString());
    this->ui->Display_UID->setText(query.value("UID").toString());
    this->ui->Display_Grade->setText(query.value("Grade").toString());
    this->ui->Display_Class->setText(query.value("Class").toString());
    this->ui->Line_Phone->setText(query.value("Phone").toString());
    this->ui->Display_Academy->setText(query.value("Academy").toString());
    this->ui->Display_Major->setText(query.value("Major").toString());
    this->ui->Display_Sex->setText(query.value("Sex").toString());
    this->ui->Display_Politics->setText(query.value("Policy").toString());

    if(modify){ //修改状态
        this->ui->Line_Pwd->clear();
        this->ui->Line_Pwd_2->clear();
        this->ui->Line_Name->setReadOnly(false);
        this->ui->Line_Phone->setReadOnly(false);
        this->ui->Line_Pwd->setVisible(true);
        this->ui->label_Pwd->setVisible(true);
        this->ui->Line_Pwd_2->setVisible(true);
        this->ui->label_Pwd_2->setVisible(true);
        this->ui->Btn_Exit->setText("取消");
        this->ui->Btn_Update->setText("确定");

    }
    else{   //查询状态只读
        this->ui->Line_Name->setReadOnly(true);
        this->ui->Line_Phone->setReadOnly(true);
        this->ui->Line_Pwd->setVisible(false);
        this->ui->label_Pwd->setVisible(false);
        this->ui->Line_Pwd_2->setVisible(false);
        this->ui->label_Pwd_2->setVisible(false);
        this->ui->Btn_Exit->setText("退出");
        this->ui->Btn_Update->setText("修改");
    }
    setCursor(Qt::ArrowCursor);
}

void student::on_Btn_Exit_clicked()
{
    if(modify){ //取消修改
        modify = false;
        this->showinfo();
    }
    else
        this->close();
}
